﻿using System.Linq;
using System;
using System.Collections.Generic;
using RSSReader.classesCustom;

namespace FunctionalityTests
{
    class TestDataHandler
    {
        List<Article> artlist;
        public DataHandler dh = new DataHandler("Data Source=10.80.83.32\\sql2005;" +
            "Initial Catalog=kaaTestDB;User ID=sa;Password=sql2005");

        public void runTests()
        {
            //this.testSQLData();
            //this.testSQLData2();
            //this.testXMLData();
            //this.testSqlWriteData();
            //this.test_setSiteVisibilityforUser();
            //this.test_ObjectReferences();
            this.test_ArticlePagedFilter();
            //this.test_ArticleSorter();
        }

        private void testSQLData()
        {
            artlist = dh.getSQLArticles(3, 6);
            foreach (Article art in dh.getSQLArticles())
            {
                Console.WriteLine("object here: {0} , {1}", art.ID, art.title);
            }
            Console.ReadKey();
        }

        private void testSQLData2()
        {
            KeyValuePair<Int32, List<Article>> artlist = dh.getUserCustomSQLArticles(0, 5, 1);
            Console.WriteLine("page count: {0} , total count {1}", artlist.Value.Count, artlist.Key);
            foreach (Article art in artlist.Value)
            {
                Console.WriteLine("object here: {0} , {1}", art.ID, art.title);
            }
            Console.ReadKey();
        }

        private void testXMLData()
        {
            artlist = dh.getSQLArticles();
            foreach (Article art in dh.onlineArticles)
            {
                Console.WriteLine(art);
            }
            Console.ReadKey();
        }

        private void testSqlWriteData()
        {
            dh.loadXMLArticles();
            dh.saveXMLArticleToDatabase(15, 1);
        }

        private void test_setSiteVisibilityforUser()
        {
            dh.setSiteVisibilityforUser(1, 2, true);
            dh.setSiteVisibilityforUser(1, 2, false);
            dh.setSiteVisibilityforUser(1, 2, true);
        }

        private void test_ObjectReferences()
        {
            Site a = new Site();
            a.ID = 1;
            a.name = "name";
            a.logo = "logo";
            a.rssAddress = "rssAddress";
            a.description = "description";
            a.visible = true;

            Site b = new Site(a);
            Console.WriteLine("a b reference {0}", object.ReferenceEquals(b, a));
            Console.WriteLine("ID a b reference {0}", object.ReferenceEquals(b.ID, a.ID));
            Console.WriteLine("name a b reference {0}", object.ReferenceEquals(b.name, a.name));
            b.name = "nam";
            Console.WriteLine("name a b reference {0}", object.ReferenceEquals(b.name, a.name));
            Console.WriteLine("visibility a b reference {0}", object.ReferenceEquals(b.visible, a.visible));
        }

        private void test_ArticlePagedFilter()
        {            
            ArticleCriteriaInString sfilter =
                new ArticleCriteriaInString("5", "100", "", "", "", "");            
            ArticleFilter filter = new ArticleFilter(sfilter, dh.onlineArticles);
            
            ArticlePager pager = new ArticlePager(sfilter, filter.getFilteredArticleList());

            foreach (Article a in pager.getArticlePage())
                Console.WriteLine(a);            
            Console.ReadLine();

        }

        private void test_ArticleSorter()
        {
            ArticleSorter arts =new ArticleSorter(null,dh.onlineArticles);

            List<Article> l=arts.getSortedList();
            foreach (Article a in l)
                Console.WriteLine(""+a.ID+a.category+a.logo);
        }

    }
}
